﻿#region <!-- Using Directives -->

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using WordSharpDev.Extensions;

using Word = Microsoft.Office.Interop.Word;

#endregion

namespace WordSharpDev.Controls
{
	public partial class ActionPane : UserControl
	{
		#region <!-- Switch Status -->

		public void SwitchStatusReady()
		{
			TextLblMainLogs.Style = (Style)Resources["StatusReady"];
			
			TextLblMainLogs.Refresh();
		}

		public void SwitchStatusWait()
		{
			TextLblMainLogs.Style = (Style)Resources["StatusWait"];

			TextLblMainLogs.Refresh();
		}

		public void SwitchStatusError()
		{
			TextLblMainLogs.Style = (Style)Resources["StatusError"];

			TextLblMainLogs.Refresh();
		}

		#endregion

		#region <!-- Document Section -->

		public event Action OnDocPropChange;

		public void ShowDocumentTab()
		{
			if (!TabItemDocument.IsSelected) { TabItemDocument.IsSelected = true; TabItemDocument.Refresh(); }
		}

		public void NotifyDocumentSelected(XmlNode p_Document)
		{
			TreeViewDocBase.SelectXmlDocument(p_Document);

			TreeViewDocBase.Refresh();
		}

		public void NotifyDocumentMerged(XmlNode p_Document)
		{
			TreeViewDocBase.MergeXmlDocument(p_Document);

			TreeViewDocBase.Refresh();
		}

		public void ReloadDocumentCount()
		{
			TextLblDocCount.Text = String.Format("- {0:##,#0} Document(s) -", Share.Project.CountDocuments());

			TextLblDocCount.Refresh();
		}

		public void ReloadDocumentBase()
		{
			foreach (var l_Document in Share.Project.GetDocuments())
			{
				TreeViewDocBase.MergeXmlDocument(l_Document);
			}

			TreeViewDocBase.Refresh();
		}

		public String DocumentProperties
		{
			get
			{
				return TextBoxDocProp.Text = TextBoxDocProp.Text.FormatXml();
			}

			set
			{
				TextBoxDocProp.Text = value;

				TextBoxDocProp.Text = TextBoxDocProp.Text.FormatXml();

				TextBoxDocProp.Refresh();
			}
		}

		public Boolean IsDocumentSaved
		{
			set
			{
				TextLblDocSaved.Text = value ? "- Saved -" : "* Unsaved *";

				TextLblDocSaved.Refresh();
			}
		}

		#endregion

		#region <!-- Code Section -->

		public event Action OnCodeDescChange;

		public void ShowCodeTab()
		{
			if (!TabItemCode.IsSelected) { TabItemCode.IsSelected = true; TabItemCode.Refresh(); }
		}

		public String CodeDescription
		{
			get
			{
				return TextBoxCodeDesc.Text = TextBoxCodeDesc.Text.FormatXml();
			}

			set
			{
				TextBoxCodeDesc.Text = value;

				TextBoxCodeDesc.Text = TextBoxCodeDesc.Text.FormatXml();

				TextBoxCodeDesc.IsEnabled = !TextBoxCodeDesc.Text.IsVoid();

				TextBoxCodeDesc.Refresh();
			}
		}

		public Word.Table CodeTarget { get; set; }

		#endregion

		#region <!-- Logs Section -->

		public void ShowDetailLogsTab()
		{
			if (!TabItemDetailLogs.IsSelected) { TabItemDetailLogs.IsSelected = true; TabItemDetailLogs.Refresh(); }
		}

		public void WriteMainLogs(String p_Logs)
		{
			TextLblMainLogs.Text = String.Format("{0}", p_Logs);

			TextLblMainLogs.Refresh();
		}

		public void WriteDetailLogs(String p_Logs)
		{
			if (32768 < TextBoxDetailLogs.Text.Length)
			{
				TextBoxDetailLogs.Text = "";
			}

			TextBoxDetailLogs.Text += String.Format("{0:HH:mm:ss}", DateTime.Now) + "\r\n";
			TextBoxDetailLogs.Text += p_Logs.Indent("\t") + "\r\n";

			TextBoxDetailLogs.Refresh();
		}

		#endregion

		#region <!-- ActionPane -->

		public ActionPane()
		{
			InitializeComponent();

			TextBoxDocProp.GotFocus += (p_Sender, p_Args) =>
			{
				TextBoxDocProp.Text = TextBoxDocProp.Text.FormatXml();
			};

			TextBoxDocProp.LostFocus += (p_Sender, p_Args) =>
			{
				TextBoxDocProp.Text = TextBoxDocProp.Text.FormatXml();

				if (OnDocPropChange != null) OnDocPropChange();
			};

			TextBoxCodeDesc.GotFocus += (p_Sender, p_Args) =>
			{
				TextBoxCodeDesc.Text = TextBoxCodeDesc.Text.FormatXml();
			};

			TextBoxCodeDesc.LostFocus += (p_Sender, p_Args) =>
			{
				TextBoxCodeDesc.Text = TextBoxCodeDesc.Text.FormatXml();

				if (OnCodeDescChange != null) OnCodeDescChange();
			};

			TextBoxDocSearch.KeyDown += KeyDownIntercept;
			TextBoxDocProp.KeyDown += KeyDownIntercept;

			TextBoxCodeDesc.KeyDown += KeyDownIntercept;

			TextBoxDetailLogs.KeyDown += KeyDownIntercept;
		}

		#endregion

		#region <!-- KeyDownIntercept -->

		private void KeyDownIntercept(Object p_Sender, KeyEventArgs p_Args)
		{
			if (p_Args.KeyboardDevice.Modifiers == ModifierKeys.Control)
			{
				if (p_Args.Key == Key.S)
				{
					Share.Application.ActiveDocument.Save();
				}
			}
		}

		#endregion
	}
}